package com.video.web.controller.video;

import com.video.common.exception.ServiceException;
import com.video.video.domain.dto.DGroupItemDto;
import com.video.video.domain.vo.GroupItemQuery;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.video.common.annotation.Log;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.enums.BusinessType;
import com.video.video.domain.DGroupItem;
import com.video.video.service.IDGroupItemService;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 分组明细Controller
 *
 * @author ruoyi
 * @date 2022-09-30
 */
@RestController
@RequestMapping("/video/GroupItem")
public class DGroupItemController extends BaseController
{
    @Autowired
    private IDGroupItemService dGroupItemService;

    /**
     * 查询分组明细列表
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(DGroupItem dGroupItem)
    {
        startPage();
        List<DGroupItem> list = dGroupItemService.selectDGroupItemList(dGroupItem);
        return getDataTable(list);
    }

    /**
     * 查询分组明细列表
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:listDto')")
    @GetMapping("/listDto")
    public TableDataInfo listDto(GroupItemQuery query)
    {
        startPage();
        List<DGroupItemDto> list = dGroupItemService.selectDGroupItemlistDto(query);
        return getDataTable(list);
    }

    /**
     * 导出分组明细列表
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:export')")
    @Log(title = "分组明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DGroupItem dGroupItem)
    {
        List<DGroupItem> list = dGroupItemService.selectDGroupItemList(dGroupItem);
        ExcelUtil<DGroupItem> util = new ExcelUtil<DGroupItem>(DGroupItem.class);
        util.exportExcel(response, list, "分组明细数据");
    }

    /**
     * 获取分组明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dGroupItemService.selectDGroupItemById(id));
    }

    /**
     * 新增分组明细
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:add')")
    @Log(title = "分组明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DGroupItem dGroupItem)
    {
        //查看当前商品有没有添加到当前分组
        DGroupItem item=new DGroupItem();
        item.setGroupid(dGroupItem.getGroupid());
        item.setGoodid(dGroupItem.getGoodid());
        List<DGroupItem> list= dGroupItemService.selectDGroupItemList(item);
        if(CollectionUtils.isNotEmpty(list)){
            throw new ServiceException("该商品已经添加到当前分组");
        }
        Long userId = getLoginUser().getUserId();
        dGroupItem.setCreateBy(String.valueOf(userId));
        return toAjax(dGroupItemService.insertDGroupItem(dGroupItem));
    }

    /**
     * 修改分组明细
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:edit')")
    @Log(title = "分组明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DGroupItem dGroupItem)
    {
        return toAjax(dGroupItemService.updateDGroupItem(dGroupItem));
    }

    /**
     * 删除分组明细
     */
    @PreAuthorize("@ss.hasPermi('video:GroupItem:remove')")
    @Log(title = "分组明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dGroupItemService.deleteDGroupItemByIds(ids));
    }
}
